package com.tac.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.tac.common.result.Result;
import com.tac.pojo.Slide;
import com.tac.service.SlideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/slide")
@CrossOrigin
@RestController
@Slf4j
@Api(tags = "轮播图")
public class SlideController  {
    @Autowired
   private SlideService slideService;
    /**
     * 查看所有轮播图
     * @param page
     * @param pagesize
     * @return
     */
    @ApiOperation("管理员查看所有轮播图")
    @GetMapping
    public Result<Page<Slide>> selectAll(Long page, Long pagesize, HttpServletRequest request){
        log.info("用户：{}，通过地址{}--请求查看所有轮播图",request.getRemoteAddr(),request.getRequestURI());
        return  slideService.selectAll(page,pagesize);
    }

    /**
     * 添加轮播图
     * @param slide
     * @return
     */
    @ApiOperation("添加轮播图")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Result saveSlide(Slide slide, HttpServletRequest request){
        log.info("用户：{}，通过地址{}--请求添加轮播图",request.getRemoteAddr(),request.getRequestURI());
        slide.setId(null);
        return slideService.saveSlide(slide);
    }

    /**
     * 修改轮播图
     * @param slide
     * @return
     */
    @ApiOperation("修改轮播图")
    @PutMapping
    public Result updateSlide(Slide slide){
        return slideService.updateSlide(slide);
    }

    /**
     * 删除
     * @param ids
     * @return
     *
     */
    @ApiOperation("删除")
    @DeleteMapping
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Result deleteById(List<Long> ids){
        return slideService.deleteById(ids);
    }
    /**
     * 状态切换启用或停用
     * @param id
     * @return
     */
    @PutMapping("/{id}/{status}")
    @ApiOperation("状态切换启用或停用")
    public  Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        return slideService.updateStatus(id,status);
    }

    /**
     * 查看首页轮播图
     * @return
     */
    @ApiOperation("查看首页轮播图")
    @GetMapping("/selectSlide")
    public Result<List<Slide>> selectSlide(){
        return slideService.selectSlide();
    }

    /**
     * 查看分类
     * @return
     */
    @ApiOperation("查看分类")
    @GetMapping("/selectType")
    public Result<List<String>> selectType(){
        return slideService.selectType();
    }

}
